package com.smart.translator.util;

import android.util.Log;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class WechatLogcatMonitor {
    private static final String TAG = "WechatLogcatMonitor";
    private static Process logcatProcess;
    private static Thread logcatThread;
    private static volatile boolean running = false;

    /**
     * 启动logcat监听，输出所有logcat日志
     */
    public static void start() {
        if (running) return;
        running = true;
        logcatThread = new Thread(() -> {
            try {
                // 直接抓取所有logcat日志（不加grep）
                String cmd = "logcat";
                logcatProcess = Runtime.getRuntime().exec(cmd);
                BufferedReader reader = new BufferedReader(new InputStreamReader(logcatProcess.getInputStream()));
                String line;
                while (running && (line = reader.readLine()) != null) {
                    Log.d(TAG, "logcat: " + line);
                }
            } catch (Exception e) {
                Log.e(TAG, "logcat监听异常", e);
            }
        });
        logcatThread.start();
    }

    /**
     * 停止logcat监听
     */
    public static void stop() {
        running = false;
        if (logcatProcess != null) {
            logcatProcess.destroy();
            logcatProcess = null;
        }
        if (logcatThread != null) {
            logcatThread.interrupt();
            logcatThread = null;
        }
    }
} 